package com.api.gbhg.daiyanlu.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.daiyanlu.Dao.AccountingPushedDao;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * 会计角色待推送
 * Mr.dyl
 * 2021.04.08
 */
@Path("/accounting")
public class AccountingPushed {
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    AccountingPushedDao dao=new AccountingPushedDao();
    @POST
    @Path("/getAccounting")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getAccounting(Map<String,Object> param) throws Exception {
        String requestId = param.get("requestId").toString();
        Integer pageNum = 1;//当前页面
        Integer pageSize = 5;//显示数量
        if (param.containsKey("pageNum") && param.get("pageNum") instanceof Integer && (Integer) param.get("pageNum") > 0)
            pageNum = (Integer) param.get("pageNum");
        if (param.containsKey("pageSize") && param.get("pageSize") instanceof Integer && (Integer) param.get("pageSize") > 0)
            pageSize = (Integer) param.get("pageSize");
        int nd = getNd(param);
        String dw = "2";
        if (param.containsKey("dw") && param.get("dw") instanceof Integer) {
            dw = AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));
        }
        //根据requestId查询workflow_currentoperator表workflowid
        RecordSetTrans rs = dao.getRequestIdDao(requestId);
        Map<String, Object>[] map = new HashMap[rs.getArray().size()];
        int i = 0;
        int total=0;
        while (rs.next()){
            map[i] = new HashMap<>();
            //根据workflow_currentoperator表的workflowid查找workflow_base表的id
            RecordSetTrans subTable = dao.getIdDao(rs.getString("workflowid"));
            while (subTable.next()){
                map[i] = new HashMap<>();
                //根据workflow_bill的id查找表名
                RecordSetTrans mainTable = dao.getFromId(subTable.getString("formid"));
                while (mainTable.next()){
                    map[i] = new HashMap<>();
                    /**
                     * 报销流程主表
                     */
                    RecordSetTrans es = dao.getAccountingDao(mainTable.getString("tablename"),pageNum,pageSize,nd,dw);
                    total = dao.getAccountingCount(mainTable.getString("tablename"),nd,dw);
                    while (es.next()) {
                        map[i] = new HashMap<>();
                        map[i].put("bxdbh", es.getString("lcbh"));//报销单编号
                        map[i].put("bxrq", "2021-03-02");//报销日期
                        RecordSet jbr = rmcd.getUserNameById(mainTable.getInt("jbr"));
                        jbr.next();
                        String loginid = jbr.getString("loginid");
                        if(loginid==""){
                            loginid="系统管理员";
                        }
                        map[i].put("jbr",loginid);  //经办人
                        i++;
                    }
                    /**
                     * 报销流程明细表
                     */
//                    RecordSetTrans est = dao.getAccountingDetail(mainTable.getString("tablename"),pageNum,pageSize,nd,dw);
//                    while (est.next()) {
//                        map[i] = new HashMap<>();
//                        map[i].put("zfl",est.getString("zfl"));//支付令
//                        map[i].put("ysly", est.getString("ysly"));//预算来源
//                        map[i].put("ysxm", est.getString("ysxm"));//预算项目
//                        i++;
//                    }
                    i++;
                }
                i++;
            }
            i++;
        }
        com.api.gbhg.zengping.page.PageInfo<String> pageInfo = new com.api.gbhg.zengping.page.PageInfo<>();
        pageInfo.setList(map);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(total);
        return JSON.toJSONString(pageInfo);
    }


    public int getNd(Map<String,Object> param){
        if(param.containsKey("nd") && param.get("nd") instanceof Integer )
            return  (Integer) param.get("nd");
        else
            return LocalDate.now().getYear();
    }
}
